#!/bin/sh /etc/rc.common
# Copyright (c) 2011-2012 OpenWrt.org
START=80

resolve_ifname() {
	grep -qs "^ *$1:" /proc/net/dev && {
		append ifaces "$1"
	}
}

resolve_network() {
	local ifn
	fixup_interface "$1"
	config_get ifn "$1" ifname
	[ -z "$ifn" ] && return 1
	resolve_ifname "$ifn"
}

start_neighbor6() {
	local args="-s -l"
	local ifaces=""

	SERVICE_DAEMONIZE=1
	SERVICE_WRITE_PID=1
	SERVICE_PID_FILE="/var/run/neighbor6-$cfg.pid"
	[ -f "$SERVICE_PID_FILE" ] && {
		if grep -q neighbor6 "/proc/$(cat $SERVICE_PID_FILE)/cmdline"; then
			return 0
		else
			rm -f "$SERVICE_PID_FILE"
		fi
	}

	local net networks
	config_get networks "$cfg" network
	for net in $networks; do
		resolve_network "$net" || {
			return 1
		}
	done

	local ifn ifnames
	config_get ifnames "$cfg" ifname
	for ifn in $ifnames; do
		resolve_ifname "$ifn"
	done

	service_start /usr/sbin/neighbor6 $args $ifaces
}

stop() {
	for pid in /var/run/neighbor6-*.pid; do
		SERVICE_PID_FILE="$pid"
		service_stop /usr/sbin/neighbor6
		rm -f "$SERVICE_PID_FILE"
	done
}

start() {
	include /lib/network
	config_load neighbor6
	config_foreach start_neighbor6
}



